home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / educate / wordy402.zip / ANAGRAM.C < prev    next >
C/C++ Source or Header  |  1995-12-17  |  7KB  |  252 lines

  1. /**************************************************************************/
  2. /*                             Anagram Utility                            */
  3. /*                                                                        */
  4. /*                                 M\Cooper                               */
  5. /*                          3425 Chestnut Ridge Rd.                       */
  6. /*                        Grantsville, MD 21536-9801                      */
  7. /*                        --------------------------                      */
  8. /*                        Email:  thegrendel@aol.com                      */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package              */
  11. /*                                                                        */
  12. /**************************************************************************/
  13.  
  14.  
  15. #include <conio.h>
  16. #include "srch.h"
  17.  
  18.  
  19. #define FILE_OPENING_ERROR 3
  20. #define FILENAME_MAXLEN 8
  21. #define CR "\n"
  22. #define FILE_SUFFIX ".wds"
  23. #define MAXLEN 40
  24. #define LINE_LEN 80
  25. #define NOARGS 1
  26. #define INCREMENT 1
  27. #define SPACE ' '
  28. #define XOUT '@'
  29. #define WILDCARD '?'
  30. #define FILLCHAR '_'
  31. #define WD 12
  32.  
  33. #define BUFFERSIZE 8192
  34.  
  35.    char ad[] =
  36. "ANAGRAM utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536-9801";
  37.  
  38.  
  39. void getword( char *lset );
  40. void center( char *strng );
  41.  
  42. typedef enum { FALSE, TRUE } Boolean;
  43.  
  44. void main( int argc, char **argv )
  45. {
  46.  
  47.    char letterset[ MAXLEN ];
  48.  
  49.      if( argc == NOARGS )
  50.         {
  51.         clrscr();
  52.         puts("Enter a LETTERSET to test ... ");
  53.         gets( letterset );
  54.         }
  55.      else
  56.         strcpy( letterset, *( argv + 1 ) );
  57.  
  58.      getword( letterset );
  59. }
  60.  
  61.  
  62. /**********************************WORDTEST********************************/
  63. /*       Function tests if word is constructible from Letterset           */
  64. /*                 Args in: char *letterset, char *word                   */
  65. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  66. /**************************************************************************/
  67.  
  68. Boolean wordtest( char *letterset, char *word )
  69. {
  70.     Boolean error_flag = TRUE;
  71.     static char dup_lset[ MAXLEN ];
  72.     register char *letpos;
  73.  
  74. //     dup_lset = strdup( letterset );
  75.      strcpy( dup_lset, letterset );
  76.          
  77.         while( *word )
  78.             {
  79.             if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
  80.                 *letpos = XOUT;     //As long as letter contained...
  81.       else
  82.          if( ( letpos = strchr( dup_lset, WILDCARD ) ) != NULL ) 
  83.             *letpos = XOUT;  //Or wildcard character...
  84.  
  85.             else
  86.                 { error_flag = FALSE; break; } //test fails (not contained)
  87.             }
  88.  
  89.         return( error_flag );
  90. }
  91.  
  92. /*************************************************************/
  93. void getword( char *letter_set )
  94. {
  95.  
  96.     char    l_set [ MAXLEN ],
  97.         word [ MAXLEN ],
  98.         tempstr [ MAXLEN + 1 ],
  99.         targetfile [ MAXLEN ],
  100.         bar [ LINE_LEN + 1 ],
  101.         double_bar [ LINE_LEN + 1 ],
  102.   msg [ WD ],
  103.    *ppos;
  104.  
  105.     FILE *fptr,
  106.         *tfile;
  107.     int fnamelen,
  108.      i;
  109.     long wcount = 0L;
  110.  
  111.        memset( bar, '-', LINE_LEN );
  112.        *( bar + LINE_LEN ) = NULL;
  113.        memset( double_bar, '=', LINE_LEN );
  114.        *( double_bar + LINE_LEN ) = NULL;
  115.  
  116.        /*************opening credits*************/
  117.        clrscr();
  118.        printf( double_bar );
  119.        strcpy( tempstr, ad );
  120.        center ( tempstr );
  121.        printf( tempstr );
  122. //   printf( CR );
  123.        printf( double_bar );
  124.        printf( CR );
  125.        /****************************************/
  126.  
  127.  
  128.        strcpy ( l_set, letter_set );
  129.        strcat ( letter_set, CR );
  130.  
  131.        /*   Create name of file to store derived words in   */
  132.        /*********************************************************/
  133.        fnamelen = strlen( l_set );
  134.        if( fnamelen  > FILENAME_MAXLEN )
  135.           fnamelen = FILENAME_MAXLEN;
  136.        strncpy( targetfile, l_set, fnamelen );
  137.        *( targetfile + fnamelen ) = NULL;
  138.        //NULL-terminate string, so strcat works, ha, ha.
  139.  
  140.       for( i = 0; i < fnamelen; i++ )
  141.          if( *( targetfile + i ) == WILDCARD )
  142.              *( targetfile + i ) = FILLCHAR;
  143.  
  144.  
  145. //    if( ( ppos = strchr( targetfile, '?' ) ) != NULL ) 
  146. //          *ppos = '0';
  147. //           strcpy( targetfile, "wildcard" );
  148.  
  149. //    ppos = targetfile;
  150. //    while( *ppos != NULL )
  151. //       if( *ppos == '?' )
  152. //          {
  153. //          *ppos = FILLCHAR;
  154. //          ppos++;
  155. //          printf( "\7\7\7\7" );  /******DEBUG******/
  156. //          }
  157.  
  158.        strcat( targetfile, FILE_SUFFIX );
  159.        /*********************************************************/
  160.  
  161.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  162.          {
  163.          printf( "\7\7\7Cannot open Wordfile!" );
  164.          exit( FILE_OPENING_ERROR );
  165.          }
  166.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE * 2 ) )
  167.          exit ( FILE_OPENING_ERROR );  //Extra buffering.
  168.  
  169.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  170.          {
  171.          printf( "\7\7\7Cannot open file to save words in!" );
  172.          exit ( FILE_OPENING_ERROR + 1 );
  173.          }
  174.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  175.          exit( FILE_OPENING_ERROR );  //Extra buffering.
  176.  
  177.  
  178.        /**************'Wait' Message************/
  179.        printf( CR CR );
  180.        printf( "WORKING...\n\n" );
  181.        printf( "This will take a few seconds \n" );
  182.        printf( "Please be patient...\n\n" );
  183.        printf( "Now searching 100,000+ word file and writing file of valid anagrams.\n\n" );
  184.        /*****************************************/
  185.  
  186.  
  187.  
  188.  
  189.  
  190.        sprintf( tempstr, "Words anagrammed from: %s\n", strupr( l_set ) );
  191.        center( tempstr );
  192.        fprintf( tfile, double_bar );
  193.       fprintf( tfile, CR );
  194.        fprintf( tfile, tempstr );
  195.        fprintf( tfile, double_bar );
  196.        fprintf( tfile, CR );
  197.  
  198.  
  199.          /*********************Main Loop*************/     
  200.           while( fgets( word, MAXLEN, fptr ) != NULL )
  201.  
  202.             if( wordtest( letter_set, word ) )
  203.                {
  204.                fprintf( tfile, "%s", word );
  205.                wcount++;
  206.                }
  207.           /*******************************************/
  208.  
  209.       if( wcount == INCREMENT )
  210.           strcpy( msg, "word" );
  211.       else
  212.           strcpy( msg, "words" );
  213.  
  214.           fprintf( tfile, bar );
  215.       fprintf( tfile, CR );
  216.           sprintf( tempstr, "%ld %s can be anagrammed from %s.",
  217.                  wcount, msg, l_set );
  218.           center( tempstr );              
  219.           fprintf( tfile, tempstr );
  220.           fprintf( tfile, "\n\n" );
  221.  
  222.           center( ad );
  223.           fprintf( tfile, ad );
  224.  
  225.           fcloseall();
  226.  
  227.           sprintf( tempstr,
  228.                  "The file %s has %ld %s anagrammed from %s\7.",
  229.                  targetfile, wcount, msg, l_set );
  230.           center( tempstr );
  231.           printf( CR CR );
  232.           printf( tempstr );
  233.     printf( CR CR );
  234.  
  235. }
  236.  
  237.  
  238.  
  239. void center( char *str )
  240. {
  241.    int padding;
  242.    char st [ LINE_LEN + INCREMENT ];
  243.  
  244.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  245.      memset( st, SPACE, padding );
  246.      *( st + padding ) = NULL;  //Terminate string
  247.      strcat( st, str );
  248.      strcpy( str, st );
  249.  
  250.      return;
  251. }
  252.